package com.lagou.service;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @Author wyx
 * @Date 2020/3/15 0:31
 */
public class RpcDecoder extends ByteToMessageDecoder {

    private Class<?> clazz;

    private Serializer serializer;

    public RpcDecoder(Class<?> clazz, Serializer serializer) {

        this.clazz = clazz;

        this.serializer = serializer;

    }


    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {

        if (byteBuf != null) {
            int length = byteBuf.readInt();
            byte[] content = new byte[length];
            byteBuf.readBytes(content);

            RpcRequest rpcRequest = serializer.deserialize(RpcRequest.class,content);

            list.add(rpcRequest);

        }
    }
}
